package cn.edu.nju.ws.sview.util;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * �û�ע����֤
 * @author ssgong
 *
 */
public class PostMail {
	private String user =null;
	private String password = null;
	private String from =null;
	private String to=null;
	private String subject=null;
	private String content=null;
	private String host=null;
	public void setHost(String host){
		this.host = host;
	}
	public void setAccount(String user,String password){
		this.user = user;
		this.password = password;
	}
	public void setMail(String from,String to,String subject,String content){
		this.from = from;
		this.to = to;
		this.subject = subject;
		this.content = content;
	}
	public void send(){
		Properties props = new Properties();
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.auth", "true");
		Session session = Session.getInstance(props) ;
		Message message = new MimeMessage(session);
		try {
			message.setFrom(new InternetAddress(from));
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
			((MimeMessage)message).setSubject(subject,"utf-8");
			message.setContent(content, "text/html;charset=utf-8");
			Transport transport = session.getTransport("smtp");
			transport.connect(host,user,password);
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();
		} catch (AddressException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		PostMail mail = new PostMail();
		mail.setHost("smtp.seu.edu.cn");
		mail.setAccount("ssgong","gss3778980");
		String from = "ssgong@seu.edu.cn";
		String to = "ssgong@seu.edu.cn" ;
		String subject = "test";
		String content = "test";
		mail.setMail(from, to, subject, content);
		mail.send();
		System.out.println("finish");
	}
	
}
